package com.jf.identity.service.impl;

import com.jf.identity.entity.ExamBaseParam;
import com.jf.identity.entity.GatherInfo;
import com.jf.identity.entity.GatherProcess;
import com.jf.identity.holder.ExamBaseParamHolder;
import com.jf.identity.mongo.IdentityGatherDao;
import com.jf.identity.service.IdentityGatherService;
import com.jf.identity.util.PostPictureUtil;
import com.jf.identity.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by x on 2018/4/14.
 */
@Service
public class IdentityGatherServiceImpl implements IdentityGatherService {
    @Resource
    private ExamBaseParamHolder examBaseParamHolder;
    @Resource
    private IdentityGatherDao identityGatherDao;
    @Override
    public boolean insertIdentityGatherProcessInfo(GatherProcess gatherProcess) {
        ExamBaseParam examBaseParam = examBaseParamHolder.getExamBaseParam();
        gatherProcess.setExamBaseParam(examBaseParam);
        gatherProcess.setUid(UUIDUtil.generateUUID());
        gatherProcess.setTimestamp(new Date());
        int n = identityGatherDao.insertIdentityGatherProcessInfo(gatherProcess);
        return n!=0;
    }

    @Override
    public boolean insertIdentityGatherInfo(List<GatherInfo> gatherInfos) {
        ExamBaseParam examBaseParam = examBaseParamHolder.getExamBaseParam();
        int n = 0;
        for (GatherInfo g:gatherInfos) {
            g.setUid(UUIDUtil.generateUUID());
            g.setExamBaseParam(examBaseParam);
            String type = null;
            if("0".equals(g.getCJLX())){
                type = "5";
            }else if ("1".equals(g.getCJLX())){
                type = "4";
            }
            //todo:图片不存入MongoDB中，先将图片存入本地，然后将TP替换成路径
            if(type != null && StringUtils.isEmpty(g.getTP())){
                String picURL  = PostPictureUtil.postPicAtLocal(g.getTP(),type,examBaseParam.getExamId());
                g.setTP(picURL);
            }
            g.setTimestamp(new Date());
            n+= identityGatherDao.insertIdentityGatherInfo(g);
        }
        return n!=0;
    }
}
